<?php

App::uses('AppModel', 'Model');

/**
 * PropertyType Model
 *
 * @property Property $Property
 * @property Project $Project
 */
class PropertyType extends AppModel {

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'name_tha' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'name_eng' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'property_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'Property' => array(
            'className' => 'Property',
            'foreignKey' => 'property_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'Project' => array(
            'className' => 'Project',
            'foreignKey' => 'property_type_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public function getAll() {
        return $this->Find('all');
    }

    public function listOptionToDropdown($propertyId = NULL) {
        $condi = array();
        if($propertyId != NULL){
            $condi = $condi+array('PropertyType.property_id' => $propertyId);
        }
        $data = $this->Find('list', array(
            'fields' => array(
                'PropertyType.name_eng'
            ),
            'conditions' => $condi,
            'order' => array(
                'PropertyType.name_eng DESC'
            )
        ));
        
        return $data;
    }

    public function listAll() {
        $data = $this->Find('all', array(
            'recursive' => -1
        ));
        return $data;
    }

}
